package com.csmz.mybookreader.View;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.csmz.mybookreader.DBSQLite.DbMySQLite;
import com.csmz.mybookreader.DBSQLite.MyHelper;
import com.csmz.mybookreader.R;
import com.csmz.mybookreader.UI.LoginActivity;
import com.csmz.mybookreader.UI.MyMessageActivity;
import com.csmz.mybookreader.UI.SettingActivity;
import com.csmz.mybookreader.UserBean.UserInfo;
import com.csmz.mybookreader.Utils.AnalysisUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/9.
 */

public class MyInfoView {
    private static final String TAG="MyInfoView";
    public ImageView iv_head_icon;
    private LinearLayout ll_head;
    private RelativeLayout rl_setting;
    private TextView tv_user_name;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;
    private String nickname;
    public MyInfoView(Activity context){
        mContext=context;
        //为之后将Layout转化为view时用
        mInflater = LayoutInflater.from(mContext);
    }
    private void createView(){
        initView();
    }

    /**
     * 获取界面控件
     */
    private void initView() {
        //设置布局文件
        mCurrentView = mInflater.inflate(R.layout.main_view_myinfo,null);
        ll_head=mCurrentView.findViewById(R.id.ll_head);
        iv_head_icon=mCurrentView.findViewById(R.id.iv_head_icon);
        rl_setting=mCurrentView.findViewById(R.id.rl_setting);
        tv_user_name=mCurrentView.findViewById(R.id.tv_user_name);
        //tv_user_name.setText(AnalysisUtils.readLoginUserName(mContext));
        ll_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断是否已经登录
                if (readLoginStatus()){
                    //已经登录跳转到个人资料页面
                    Intent intent=new Intent(mContext, MyMessageActivity.class);
                    mContext.startActivity(intent);
                }else {
                    //未登录跳转到登录页面
                    Intent intent=new Intent(mContext,LoginActivity.class);
                    mContext.startActivityForResult(intent,1);
                }
            }
        });

        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){
                     //跳转到设置页面
                    Intent intent=new Intent(mContext, SettingActivity.class);
                    mContext.startActivityForResult(intent,1);
                     }else { Toast.makeText(mContext,"您还未登录，请先登录", Toast.LENGTH_SHORT).show();}
            }
        });


    }
    /**
     * 登录成功后设置我的界面
     * @return
     */
    public void setLoginParams(boolean isLogin) {
        if (isLogin){
            String userName=AnalysisUtils.readLoginUserName(mContext);
            //readLoginToSQL();
            tv_user_name.setText(userName);
        }else {
            tv_user_name.setText("点击登录");
        }
    }

    /**
     * 获取当前在导航栏上方显示对应的view
     */
    public View getView(){
        if (mCurrentView == null){
            createView();
        }
        return mCurrentView;}

    /**
     * 显示在当前导航栏上显示对应的view
     */
    public void showView(){
        if (mCurrentView == null) {
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }


    //从SharedPreferences中读取登录用户名
    private boolean readLoginStatus() {
        SharedPreferences sp = mContext.getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
        boolean isLogin=sp.getBoolean("isLogin",false);
        return isLogin;
    }
    //从数据库中读取登录用户名
    private void readLoginToSQL() {
        MyHelper helper=new MyHelper(mContext);
        SQLiteDatabase database=helper.getReadableDatabase();
        Cursor cursor=database.query("UserInfo",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            nickname=cursor.getString(cursor.getColumnIndex("nickname"));
            //Log.d(TAG, "readLoginToSQL: "+nickname);
        }
    }

}
